package com.hcb.micro.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.Product;
import com.hcb.micro.dto.TerracePrice;
import com.hcb.micro.service.IMallService;
import com.hcb.micro.service.IProductService;
import com.hcb.micro.service.ITerracePriceService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("terracePrice")
public class TerracePriceController extends BaseControllers{

	@Autowired
	IMallService mallService;
	
	@Autowired
	IProductService productService;
	
	@Autowired
	ITerracePriceService terracePriceService;
	

	@RequestMapping(value = "otherPriceList", method = RequestMethod.POST)
	@ResponseBody
	public String otherPriceList() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("productId") != null && !"".equals(bodyInfo.get("productId"))) {
			para.put("productId", bodyInfo.getString("productId"));
		}
		if(bodyInfo.get("name") != null && !"".equals(bodyInfo.get("name"))) {
			para.put("name", bodyInfo.getString("name"));
		}
		
		List<Map<String, Object>> otherPriceListAll=terracePriceService.selectProPriByPara(para);

		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}

		List<Map<String, Object>> otherPriceList=terracePriceService.selectProPriByPara(para);

		if(otherPriceList.size() <= 0){
			json.put("result", "1");
			json.put("description", "没有其他平台商品价格信息！");
			return buildReqJsonObject(json);
		}
		
		for(Map<String,Object> maps:otherPriceList){
			Product prod=productService.selectByPrimaryKey(Integer.valueOf(maps.get("productId")+""));
			maps.put("createTime",maps.get("createTime")+"");
			maps.put("name", prod.getName());
		}
		
    	json.put("result", "0");
		json.put("description", "获取其他平台商品价格列表成功");
		json.put("list", otherPriceList);
		json.put("count", otherPriceListAll.size());
		return buildReqJsonObject(json);
	}
	
	
	
	@RequestMapping(value = "saveOtherPrice", method = RequestMethod.POST)
	@ResponseBody
	public String saveOtherPrice() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int trues=0;
		if (bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))) {
			trues=1;
		}if(bodyInfo.get("productPrice") == null || "".equals(bodyInfo.get("productPrice"))) {
			trues=1;
		}if(bodyInfo.get("productId") == null || "".equals(bodyInfo.get("productId"))) {
			trues=1;
		}if(bodyInfo.get("cityName") == null || "".equals(bodyInfo.get("cityName"))) {
			trues=1;
		}
		if(trues==1){
			json.put("result", "1");
			json.put("description", "请检查参数输入是否正确");
			return buildReqJsonObject(json);
		}
		int res=0;
		if(bodyInfo.getInt("id")==0){
			TerracePrice trp=new TerracePrice();
			trp.setProductId(bodyInfo.getInt("productId"));
			trp.setCityName(bodyInfo.getString("cityName"));
			trp.setPrice(Float.valueOf(bodyInfo.getString("productPrice")));
			trp.setCreateTime(new Date());
			res=terracePriceService.insertSelective(trp);
		}else{
			TerracePrice trp=new TerracePrice();
			trp.setId(bodyInfo.getInt("id"));
			trp.setPrice(Float.valueOf(bodyInfo.getString("productPrice")));
			res=terracePriceService.updateByPrimaryKeySelective(trp);
		}

		if(res==1){
			json.put("result", "0");
			json.put("description", "操作成功！");
		}else{
			json.put("result", "1");
			json.put("description", "操作失败！");
		}
		return buildReqJsonObject(json);
	}
	
	
	
	@RequestMapping(value = "selProPriById", method = RequestMethod.POST)
	@ResponseBody
	public String selProPriById() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int trues=0;
		if (bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))) {
			trues=1;
		}
		if(trues==1){
			json.put("result", "1");
			json.put("description", "请检查参数输入是否正确");
			return buildReqJsonObject(json);
		}
		TerracePrice trp=terracePriceService.selectByPrimaryKey(bodyInfo.getInt("id"));
		Product prod=productService.selectByPrimaryKey(trp.getProductId());
		
		if(trp!=null){
			json.put("result", "0");
			json.put("description", "查询成功！");
			json.put("terracePrice", trp);
			json.put("proName", prod.getName());
		}
		return buildReqJsonObject(json);
	}
	
	
	@RequestMapping(value = "deleteProPri", method = RequestMethod.POST)
	@ResponseBody
	public String deleteProPri() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		int trues=0;
		if (bodyInfo.get("id") == null || "".equals(bodyInfo.get("id"))) {
			trues=1;
		}
		if(trues==1){
			json.put("result", "1");
			json.put("description", "请检查参数输入是否正确");
			return buildReqJsonObject(json);
		}
		int res=terracePriceService.deleteByPrimaryKey(bodyInfo.getInt("id"));

		if(res==1){
			json.put("result", "0");
			json.put("description", "删除成功！");
		}else{
			json.put("result", "1");
			json.put("description", "删除失败");
		}
		return buildReqJsonObject(json);
	}
	
}
